package com.fms.gui.templateCreator.components.date;

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.util.Calendar;

import javax.swing.JFrame;

import junit.framework.TestCase;

import com.fms.TestUtils;

public class DateSelectorTest extends TestCase
{

	public void testDateSelect()
	{
		Calendar c = Calendar.getInstance();
		c.set(2009, 2, 22, 15, 34);
		DateSelector s = new DateSelector(c);
		
		
		JFrame f = TestUtils.showInFrame(s);
		
		Robot r;
		try
		{
			r = new Robot();
			r.mouseMove(f.getLocationOnScreen().x + 200, f.getLocationOnScreen().y + 200);
			r.setAutoWaitForIdle(true);
			r.mousePress(InputEvent.BUTTON1_MASK);
			r.mouseRelease(InputEvent.BUTTON1_MASK);
			r.waitForIdle();
			r.waitForIdle();

			assertTrue("Day of month incorrect:" + s.getTime().get(Calendar.DAY_OF_MONTH),
					s.getTime().get(Calendar.DAY_OF_MONTH) == 25);
			f.setVisible(false);
			
		} catch (AWTException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public void testDateSelectChangeTime()
	{
		Calendar c = Calendar.getInstance();
		c.set(2009, 2, 22, 15, 34);
		DateSelector s = new DateSelector(c);
		
		
		JFrame f = TestUtils.showInFrame(s);
		
		Robot r;
		try
		{
			r = new Robot();
			r.setAutoWaitForIdle(true);
			r.mouseMove(f.getLocationOnScreen().x + 255, f.getLocationOnScreen().y + 300);
			r.mousePress(InputEvent.BUTTON1_MASK);
			r.mouseRelease(InputEvent.BUTTON1_MASK);
			r.waitForIdle();

			assertTrue("Day of month incorrect:" + s.getTime().get(Calendar.HOUR_OF_DAY),
					s.getTime().get(Calendar.HOUR_OF_DAY) == 16);
			f.setVisible(false);
			
		} 
		catch (AWTException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public void testHandleNullEntry()
	{
		Calendar c = Calendar.getInstance();
		DateSelector s = new DateSelector(null);		
		
		JFrame f = TestUtils.showInFrame(s);
		
		assertTrue("calendar incorrect:", s.getTime().get(Calendar.YEAR) == c.get(Calendar.YEAR));
		assertTrue("calendar incorrect:", s.getTime().get(Calendar.MONTH) == c.get(Calendar.MONTH));
		assertTrue("calendar incorrect:", s.getTime().get(Calendar.DAY_OF_MONTH) == c.get(Calendar.DAY_OF_MONTH));
		assertTrue("calendar incorrect:", s.getTime().get(Calendar.HOUR_OF_DAY) == c.get(Calendar.HOUR_OF_DAY));
		assertTrue("calendar incorrect:", s.getTime().get(Calendar.MINUTE) == c.get(Calendar.MINUTE));
		f.setVisible(false);		
	}
	
	
	public void testDateNoChange()
	{
		Calendar c = Calendar.getInstance();
		c.set(2009, 2, 22, 15, 34);
		DateSelector s = new DateSelector(c);		
		
		JFrame f = TestUtils.showInFrame(s);
		
		assertTrue("calendar incorrect:", s.getTime().equals(c));
		f.setVisible(false);		
	}
}
